#!/usr/bin/env python
# coding=utf-8
# __author__ = 'Yang jiaxin'

import pandas as pd
import json


def excel_json(file):
    data_json = {"data": []}
    for ri, row in file.iterrows():
        record = {
            "id": row.triple_id,
            "articleId": row.articleId,
            "group": row.group,
            "pattern": row.pattern,
            "patternId": row.patternId,
            "relation": row.relation,
            "correct": row.correct
        }
        if pd.isnull(row.subject_labelID):
            record["subject"] = row.subject_name
        else:
            record["subject"] = {
                "pmid": row.articleId,
                "labelId": int(row.subject_labelID),
                "start": int(row.subject_start),
                "end": int(row.subject_end),
                "text": row.subject_text,
                "annotate": row.subject_annotate if pd.notnull(row.subject_annotate) else None,
                "subLabels": int(row.subject_subLabels) if pd.notnull(row.subject_subLabels) else None,
                "type": row.subject_type
            }
        if pd.isnull(row.objects_labelID):
            record["objects"] = row.objects_name
        else:
            record["objects"] = {
                "pmid": row.articleId,
                "labelId": int(row.objects_labelID),
                "start": int(row.objects_start),
                "end": int(row.objects_end),
                "text": row.objects_text,
                "annotate": row.objects_annotate if pd.notnull(row.objects_annotate) else None,
                "subLabels": row.objects_subLabels if pd.notnull(row.objects_subLabels) else None,
                "type": row.objects_type
            }

        data_json['data'].append(record)
        # if row.pattern == f"{pattern}":
        #     data_json['data'].append(record)
        # print(pattern, len(data_json['data']))
    return data_json


# patterns = [
#     '临床-诊断',
#     '临床-指标',
#     '分子-血清',
#     '临床-风险因素',
#     '分子-疫苗',
#     '临床-症状',
#     '临床-治疗',
#     '分子-生化反应',
#     '分子-单抗',
#     '临床-检查'
# ]

# 给字节的修正版json
merge = pd.read_excel('/Users/genesis/Seafile/Work/生物岛知识图谱/标注数据/肺癌/LungCancer_0711.xlsx')
data = excel_json(merge)
output_path = f'/Users/genesis/Seafile/Work/生物岛知识图谱/标注数据/肺癌/LungCancer_0711.json'
output_json = open(output_path, 'w', encoding='utf-8')
output_json.write(json.dumps(data, indent=4, ensure_ascii=False) + '\n')
output_json.flush()
output_json.close()
